from scrapy import Spider, Request
from spiderssr1.items import MovieItem
import re
class Ssr1Spider(Spider):
    name = "ssr1"
    allowed_domains = ["ssr1.scrape.center"]
    # 定义基础的url
    base_url = "https://ssr1.scrape.center"

    # 定义基础请求
    def start_requests(self):
        # 首次访问的页面url
        start_url = f'{self.base_url}/page/1'
        # 对url进行请求，并回调一个解析方法
        yield Request(start_url, callback=self.parse_index)

    def parse_index(self, response):
        # 找出想要爬取数据的所有分栏
        items = response.css('.item')
        # 循环提取每个分栏
        for item in items:
            # 定位出构建详情页的参数/detail/1
            href = item.css('a::attr(href)').extract_first()
            # 拼接详情页的访问url
            detail_url = f'{self.base_url}{href}'
            # 对详情页url进行请求，并回调解析详情页的方法
            yield Request(detail_url, callback=self.parse_detail, priority=2)

        match = re.search(r'page/(\d+)', response.url)
        if not match:return
        page = int(match.group(1)) + 1
        next_url = f'{self.base_url}/page/{page}'
        yield Request(next_url, callback=self.parse_index)

    # 解析详情页
    def parse_detail(self, response):
        item = MovieItem()
        item['name'] = response.css('a h2::text').extract_first()
        item['categories'] = response.css('.categories button span::text').extract()
        item['country'] = response.css('.info span::text').extract_first()
        item['mins'] = response.css('.info span::text').re_first(r'(\d+ 分钟)')
        item['published_at'] = response.css('.info span::text').re(r'(\d{4}-\d{2}-\d{2})')
        item['score'] = response.css('.score::text').re_first('[\d\.]+')
        item['cover'] = response.css('.cover::attr(src)').extract_first()
        item['drama'] = response.css('.drama p::text').extract_first().strip()
        item['directors'] = []
        directors = response.css('.directors .director')
        for director in directors:
            director_image = director.css('.image::attr(src)').extract_first()
            director_name = director.css('.name::text').extract_first()
            item['directors'].append({'director_image': director_image, 'director_name': director_name})

        item['actors'] = []
        actors = response.css('.actors .actor')
        for actor in actors:
            actor_image = actor.css('.image::attr(src)').extract_first()
            actor_name = actor.css('.name::text').extract_first()
            item['actors'].append({'actor_image': actor_image, 'actor_name': actor_name})

        yield item

